package com.dx.controller;

import java.io.UnsupportedEncodingException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dx.pojo.Account;
import com.dx.pojo.AccountCustom;
import com.dx.pojo.UserDetail;
import com.dx.service.AccountService;

@Controller
@RequestMapping
public class AccountController {
	@Resource 
	private AccountService accountService;
	
	//管理员登录
	@RequestMapping("/admin/loginadmin")
	public String toIndex(HttpServletRequest request,Account account,HttpSession session){
		String n=accountService.adminlogin(account);
		if("2".equals(n)){
			session.setAttribute("admin", account);
			//mv.setViewName("/admin/main");
			return "admin/matchitem";
		}
		if("3".equals(n)){
			session.setAttribute("admin", account);
			//mv.setViewName("/admin/main");
			return "admin/charts";
		}else{
			//mv.setViewName("/admin/login");
			return "admin/login";
		}
		//return mv;
		
	}
	
	//修改密码
	@RequestMapping("/user/changepwd")
	public @ResponseBody String changepwd(String oldpwd,String newpwd,HttpSession session){
		Account account=(Account) session.getAttribute("account");
		account.setPasword(oldpwd);
		AccountCustom accountCustom=new AccountCustom();
		accountCustom.setAccount(account);
		accountCustom.setNewpwd(newpwd);
		int n=accountService.updatepwd(accountCustom);
		if(n!=0){
			return "修改成功";
		}else{
			return "密码修改失败，请确认密码是否正确";
		}
		
	}
	
	
	//用户注册
	@RequestMapping("/user/Register")
	public @ResponseBody String register(Account account,UserDetail userDetail){
		account.setRoleid("1");
		int i=accountService.insertaccount(account);
		if(i==1){
			accountService.insertuser(userDetail);			
		}
		return "user/main";
	}
	
	//用户登录
	@RequestMapping("/user/loginuser")
	public @ResponseBody UserDetail loginuser(Account account,UserDetail userDetail,HttpSession session){
		int i=accountService.loginbypwd(account);
		//登录成功，在查询用户详细信息
		if(i==1){
			userDetail=accountService.selectusername(account);
			//设置session存放用户登录信息
			session.setAttribute("account", account);
			return userDetail;	
		}else{
			return null;
		}
		
	}
	
	//如果用户在别的页面登录，那就替换界面中的文本框
	@RequestMapping("/user/judgelogin")
	public @ResponseBody UserDetail judgelogin(HttpSession session,UserDetail userDetail){
		Account account=(Account) session.getAttribute("account");
		if(account!=null){
			userDetail=accountService.selectusername(account);
			return userDetail;
		}else{
			return null;
		}	
	}
	
	//用户点击注销，清空session
	@RequestMapping("/user/logout")
	@ResponseBody
    public String logout(HttpSession session){
		Account account=(Account) session.getAttribute("account");
		if(account!=null){
			session.removeAttribute("account");
			return "success";
		}else{
			return null;
		}
	}
	
	@RequestMapping("/user/userDetail")
	@ResponseBody
	public UserDetail updateuser(HttpSession session){
		Account account=(Account) session.getAttribute("account");
		UserDetail userDetail=accountService.selectusername(account);
		return userDetail;
	}
	
	@RequestMapping("/user/updateuser")
	@ResponseBody
	public String save(HttpSession session,UserDetail userDetail,HttpServletRequest req,String UserName) throws Exception{
		Account account=(Account) session.getAttribute("account");
		UserDetail userDetail1=accountService.selectusername(account);
		System.out.println(userDetail1.getUserInfoId());
		userDetail.setUserInfoId(userDetail1.getUserInfoId());
		
		System.out.println(UserName);

		int n=accountService.updateuserdetail(userDetail);
		if(n!=0){
			return "成功";
		}else{
			return "失败";
		}
		
	}
}
